﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace ChargeCard.Web.Tests.Models
{
    public static class DatabaseHelper
    {

        public static void InitializeDatabase()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ChargeCardConnection"].ConnectionString);
            StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\Sql\\InitializeDB.sql");
            string sql = sr.ReadToEnd();

            SqlCommand command = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
        }

        public static void CleanupDatabase()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ChargeCardConnection"].ConnectionString);
            StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\Sql\\CleanupDB.sql");
            string sql = sr.ReadToEnd();

            SqlCommand command = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
        }
    }
}
